-- IP白名单
local mis_uu = require('modules.commhm.utils')
local mis_env = require('modules.env.env')
local mis_timer = require('modules.commhm.ngxTimerWrap')
local uu = mis_uu
local tonumber = tonumber
local loadstring = loadstring
local os_date = os.date
local string_match = string.match

local io = io
local ngx = ngx

local mis_log = nil
local mis_config = nil
local mis_data = nil
local mis_common = nil
local mis_bridge = nil
local mis_calcu = nil
local t_retCode = nil

-- path root is nginx
local s_pathPrefix = "lua/modules/mission/config/ip_allow"

-- 业务和IP白名单映射：{work1 : {ip1 = 1, ip2 = 1, ...}, work2 : {...}}
local t_allowIp = {}

--------------------------------------------------------------
local FOO = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = FOO

local function updateAllowIp()
	local s_path = s_pathPrefix .. mis_env.debug .. ".lua"
	local t_file = io.open( s_path, "r" )
	if t_file == nil then
		mis_log.day('allow_ip_error', 'update error, config not exists|' .. s_path)
		return false
	end

	local s_config = 'do local _local_var_ =\n' .. t_file:read("*all") ..  '\nreturn	_local_var_; end';
	local f_func, s_err = loadstring(s_config)
	if type(f_func) ~= "function" then
		mis_log.day('allow_ip_error', 'update error, config content error|' .. s_path .. "|" .. uu.to_str(s_err) .. "|" .. s_config)
		return true
	end

	local ok, t_allowIpNew = pcall(f_func)
	if not ok or type(t_allowIpNew) ~= "table" then
		mis_log.day('allow_ip_error', 'update error, load content error|' .. s_path .. "|" .. uu.to_str(t_allowIpNew) .. "|" .. s_config)
		return true
	end

	-- 刷新IP配置
	t_allowIp = t_allowIpNew

	t_file:close()
	mis_log.day('allow_ip', 'update ok|' .. uu.to_str(t_allowIp) .. "|worker " .. ngx.worker.id())

	return true
end

--------------------------------------------------------------
-- 说明：定期更新白名单
local function update_loop()
	if updateAllowIp() then
		mis_timer:new():ngx_timer_at(mis_config.n_allowIpInterval, update_loop)
	end
end

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_log = t_moddules.mis_log
	mis_config = t_moddules.mis_config
	mis_data = t_moddules.mis_data
	mis_common = t_moddules.mis_common
	mis_bridge = t_moddules.mis_bridge
	mis_calcu = t_moddules.mis_calcu
	t_retCode = mis_config.t_retCode
end

--------------------------------------------------------------
-- 说明：load级初始化
this.init_load = function()
	mis_log.day('allow_ip', 'init_load! worker ' .. ngx.worker.id())
	-- if mis_calcu.is_master_server() == true then
	if true then
		update_loop()
	end
	return t_retCode.n_OK
end

--------------------------------------------------------------
-- 说明：IP是否允许
-- 参数：s_work 业务类型
-- 参数：s_ip IP
this.ip_allow = function(s_work, s_ip)
	if t_allowIp[s_work] == nil then
		mis_log.day('allow_ip_error', uu.to_str(s_work) .. '|not init|' .. uu.to_str(s_ip))
		return t_retCode.n_PARAM_ERR, uu.to_str(s_work) .. " not init"
	end

	if t_allowIp[s_work][s_ip] ~= 1 then
		return t_retCode.n_WORK_ERR, "ip not allow"
	end
	
	return t_retCode.n_OK
end

return this